home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / misc / sci / ephem_src_4_28.lha / popup.c < prev    next >
C/C++ Source or Header  |  1992-05-24  |  2KB  |  83 lines

  1. /* put up a one-line menu consisting of the given fields and let op move
  2.  * between them with the same methods as sel_fld().
  3.  * return index of which he picked, or -1 if hit END.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "screen.h"
  8.  
  9. extern void bye();
  10.  
  11. #define    FLDGAP    2    /* inter-field gap */
  12. #define    MAXFLDS    32    /* max number of fields we can handle */
  13.  
  14. static char pup[] = "Select: ";
  15.  
  16. /* put up an array of strings on prompt line and let op pick one.
  17.  * start with field fn.
  18.  * N.B. we do not do much error/bounds checking.
  19.  */
  20. popup (fields, fn, nfields)
  21. char *fields[];
  22. int fn;
  23. int nfields;
  24. {
  25.     int fcols[MAXFLDS];    /* column to use for each field */
  26.     int i;
  27.  
  28.     if (nfields > MAXFLDS)
  29.         return (-1);
  30.  
  31.     again:
  32.     /* erase the prompt line; we are going to take it over */
  33.     c_pos (R_PROMPT, C_PROMPT);
  34.     c_eol();
  35.  
  36.     /* compute starting column for each field */
  37.     fcols[0] = sizeof(pup);
  38.     for (i = 1; i < nfields; i++)
  39.         fcols[i] = fcols[i-1] + strlen (fields[i-1]) + FLDGAP;
  40.  
  41.     /* draw each field, with comma after all but last */
  42.     c_pos (R_PROMPT, 1);
  43. #ifdef AMIGA
  44.     cwrite(pup);
  45. #else
  46.     (void) fputs (pup, stdout);
  47. #endif
  48.     for (i = 0; i < nfields; i++) {
  49.         c_pos (R_PROMPT, fcols[i]);
  50. #ifdef AMIGA
  51.         cwrite(fields[i]);
  52.         if(i < nfields-1) cwrite(",");
  53. #else
  54.         printf (i < nfields-1 ? "%s," : "%s", fields[i]);
  55. #endif
  56.     }
  57.  
  58.     /* let op choose one now; begin at fn.
  59.      */
  60.     while (1) {
  61.         c_pos (R_PROMPT, fcols[fn]);
  62.         switch (read_char()) {
  63.         case END: return (-1);
  64.         case QUIT:
  65.         f_prompt ("Exit ephem? (y) ");
  66.         if (read_char() == 'y')
  67.             bye();    /* never returns */
  68.         goto again;
  69.         case REDRAW: redraw_screen(2); goto again;
  70.         case VERSION: version(); goto again;
  71.         case '\r': case ' ': return (fn);
  72.         case 'h':
  73.         if (--fn < 0)
  74.             fn = nfields - 1;
  75.         break;
  76.         case 'l':
  77.         if (++fn >= nfields)
  78.             fn = 0;
  79.         break;
  80.         }
  81.     }
  82. }
  83.